function waterFall(obj, itemSelector)
{
	if(obj.hasClass('masonry')){
		obj.masonry('reload');
	}else{
		obj.masonry({
			itemSelector: itemSelector,
			singleMode: true,
			resizeable: true,
			animate: true,
			isAnimated: true,
			animationOptions: {
				duration: 390,
				easing: 'linear',
				queue: false
			},
			isFitWidth: true
		}).addClass('masonry');
	}
}


(function($) {	
	/**
	 * @author Maowl
	 * @param {Object} options_events
	 */
	$.fn.picPortionResize = function(options_events){
		var opts = $.extend({}, 
							{	
								staticSide: 'width',	//can be 'width' or 'height'
								setting: {
									staticVal: 100,   //can be a number, or 'parent'
									anotherMax: 200,  //can be a number(if 0, no limit), or 'parent', or 'sameToStaticVal'
									anotherStaticVal: '1'
								},
								keepSmaller: false
							},
							options_events);
		
		return $(this).each(function(){
			var newImg = new Image();
			newImg.src = $(this)[0].src

			var parentObj = $(this).parent();
			var paddingWidth = (parentObj.css('padding-left').replace('px', '')*1 + parentObj.css('padding-right').replace('px', '')*1);
			var paddingHeight = (parentObj.css('padding-top').replace('px', '')*1 + parentObj.css('padding-bottom').replace('px', '')*1);
			while(parentObj.css('display') != 'block'){
				parentObj = parentObj.parent();
				//parentObj.css('display', 'block');
				paddingWidth += (parentObj.css('padding-left').replace('px', '')*1 + parentObj.css('padding-right').replace('px', '')*1);
				paddingHeight += (parentObj.css('padding-top').replace('px', '')*1 + parentObj.css('padding-bottom').replace('px', '')*1);
			}
			
			if(opts.staticSide == 'width'){
				var staticVal = (opts.setting.staticVal == 'parent')? (parentObj.width() - paddingWidth) : opts.setting.staticVal;
				if(opts.setting.anotherMax == 'parent'){
					maxHeight = parentObj.innerHeight() - paddingHeight;
				}else if(opts.setting.anotherMax == 'sameToStaticVal'){
					maxHeight = staticVal;
				}else{
					maxHeight = opts.setting.anotherMax;
				}
				if(!(newImg.width < opts.setting.staticVal && opts.keepSmaller)){
					var resizeWidth = staticVal;		
					var resizeHeight = newImg.height / newImg.width * resizeWidth;
					if(resizeHeight > maxHeight && maxHeight != 0){
						resizeHeight = maxHeight;
					}
				}
				if(opts.setting.anotherStaticVal !== null && opts.setting.anotherStaticVal !== undefined){
					resizeHeight = opts.setting.anotherStaticVal;
				}
			}else{
				var staticVal = (opts.setting.staticVal == 'parent')? (parentObj.height() - paddingHeight) : opts.setting.staticVal;
				if(opts.setting.anotherMax == 'parent'){
					maxWidth = parentObj.innerWidth() - paddingWidth;
				}else if(opts.setting.anotherMax == 'sameToStaticVal'){
					maxWidth = staticVal;
				}else{
					maxWidth = opts.setting.anotherMax;
				}
				if(!(newImg.height < opts.setting.staticVal && opts.keepSmaller)){
					var resizeHeight = staticVal;
					var resizeWidth = newImg.width / newImg.height * resizeHeight;
					if(resizeWidth > maxWidth && maxWidth != 0){
						resizeWidth = maxWidth;
					}
				}
				if(opts.setting.anotherStaticVal !== null && opts.setting.anotherStaticVal !== undefined){
					resizeWidth = opts.setting.anotherStaticVal;
				}
			}
			$(this).width(resizeWidth);
			$(this).height(resizeHeight);
		});
	};
	
	
	$.fn.relFixed = function(options_events){
		var opts = $.extend({}, 
							{	
								positionCss: {
									position: 'absolute',
									right: '2%',
									top: '2%'
								}
							},
							options_events);
		return $(this).each(function(){
			var thisObj = $(this);
			thisObj.css(opts.positionCss);
			
			var top = thisObj.position().top;
			var right = thisObj.position().right;
			thisObj.parent().scroll(function(){
				thisObj.css({
					'top': top + $(this).scrollTop() + 'px',
					'right': right + $(this).scrollLeft() + 'px'
				});
			});
		});
	};
	
	/**
	 * @author Maowl
	 * 
	 * 根据回传的json_encode形式的$_POST, 将值写回表单
	 * 
	 * usage:
	 	var FormInitModel = new FormInit(<?php echo json_encode($_POST); ?>);
		FormInitModel.transverArrtoForm();
		
		如果$_POST中有数组形式的值，表单元素需要一个和name相同的class，如: <input name="group[]" class="group" />
	 */
	window.FormInit = function(_formData){
		this.formData = _formData;
		
		this.TransverArrFunc = function (Arr, Arrvalname){
			var existCount = jQuery('.'+Arrvalname).size();
			var thisObj = this;
			
			jQuery.each(Arr, function(name, val){
				if(typeof(val) == 'object'){
					thisObj.TransverArrFunc(val, name);
				}else{
					if(Arrvalname != null){
						if(thisObj.formData['clickId'][Arrvalname] != null){
							if(name >= existCount-1){
								jQuery('#'+thisObj.formData['clickId'][Arrvalname]).click();
							}
						}
					}
					
					if(Arrvalname == null){
						jQuery('[name="'+name+'"]').val(val);
					}else{
						jQuery('.'+Arrvalname).eq(name).val(val);
					}
					
				}
			});
		};
		
		this.transverArrtoForm = function(){
			this.TransverArrFunc(this.formData, null);
		}
	};
	
	/**
	 * @author Alexander Farkas
	 * jQuery background-position animate
	 * v. 1.02
	 */
	$.extend($.fx.step,{
		backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
                        }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});

})(jQuery);




function setSize(obj, option)
{
	obj.load(function(){
		$(this).picPortionResize(option);
	}).picPortionResize(option);
}


	




$(document).ready(function(){
	setSize($('.h120MaxWparent'), {
		staticSide: 'height',
		setting: {
			staticVal: 120, 
			anotherMax: 'parent'
		},
		keepSmaller: false		
	});
	
	setSize($('.h140MaxWparent'), {
		staticSide: 'height',
		setting: {
			staticVal: 140, 
			anotherMax: 'parent'
		},
		keepSmaller: false		
	});
	
	setSize($('.h50MaxWparent'), {
		staticSide: 'height',
		setting: {
			staticVal: 50, 
			anotherMax: 'parent'
		},
		keepSmaller: false		
	});
	
	setSize($('.wfullMaxHSameToW'), {
		staticSide: 'width',
		setting: {
			staticVal: 'parent', 
			anotherMax: 'sameToStaticVal'
		},
		keepSmaller: false		
	});
	
	setSize($('.wfullH100'), {
		staticSide: 'width',
		setting: {
			staticVal: 'parent', 
			anotherStaticVal: 100
		},
		keepSmaller: false		
	});
	
	//click when hover
	$('.hoverc').mouseover(function(){
		$(this).click();
	});
	
	$('.loadClick').click();

	$('.renameInput').live('focus', function(){
		$(this).css({
			'text-align': 'left',
			'border': 'dotted #778855 1px',
			'color': '#999',
			'padding': '5px',
			'font-weight': 'normal'
		});
	}).live('blur', function(){
		$(this).css({
			'text-align': 'center',
			'border': 'none',
			'color': '#000',
			'padding': '0 0 5px 0',
			'font-weight': 'bold'
		});
	});
	
	
	$('.relFixed').relFixed();
	
	
	$('.jclound-box').jcloud({
		radius:120,
		size: 40,
		step: 1,
		speed: 180,
		flats: 2,
		clock: 100,
		areal: 100,
		splitX: 100,
		splitY: 100,
		colors:['#000000','#DD2222','#2267DD','#2A872B','#872A7B','#CAC641']
	});
	
	
	
});
